package com.hsy.linemoney.modules.exchange;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.deparse.dpframe.ui.common.DPBusEvent;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.AccountService;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.common.Consts;
import com.hsy.linemoney.modules.account.CreateSendAccountActivity;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.modules.common.StepView;

import java.util.ArrayList;
import java.util.List;

public class ExchangeStep2Activity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {

    protected StepView stepView;
    protected View indicator0;
    protected RelativeLayout recordsExists;
    protected TextView nextStep;
    protected ViewGroup contentDisplayArea;

    private AccountService.AccountListResult mAccountListResult = null;
    private int selectedAccountWhich = -1;

    private List<AccountService.AccountListResult.Value> sendAccounts = new ArrayList<>();

    @Override
    public int getRightbuttonIconResId() {
        return R.mipmap.ic_exchange_add_receiver;
    }

    @Override
    public boolean onActionBarRightButtonClicked(ImageButton rightButton) {
        ExchangeOnceOrderParamsData data = DPGsonUtil.fromJson(getActivityStartExtra()[0], ExchangeOnceOrderParamsData.class);
        startActivity(CreateSendAccountActivity.class, data.getFromCurrencyCd());
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_exchange_step2);
        setTitle(R.string.txt_pay_account);
        initView();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.next_step) {
            if (checkThrought()) {
                ExchangeOnceOrderParamsData data = DPGsonUtil.fromJson(getActivityStartExtra()[0], ExchangeOnceOrderParamsData.class);
                data.setFromAccountId(sendAccounts.get(selectedAccountWhich).getAccountId());
                startActivity(ExchangeStep3Activity.class, DPGsonUtil.toJson(data), getActivityStartExtra()[1]);
            } else {
                showToast(R.string.txt_send_account_not_selected);
            }
        } else if (view.getId() == R.id.records_exists) {
            AccountService service = new AccountService();
            service.getAccounts(new Callback<AccountService.AccountListResult>() {
                @Override
                public void onSuccess(int what, final AccountService.AccountListResult result) {
                    ExchangeOnceOrderParamsData data = DPGsonUtil.fromJson(getActivityStartExtra()[0], ExchangeOnceOrderParamsData.class);

                    ExchangeStep2Activity.this.mAccountListResult = result;
                    sendAccounts.clear();
                    sendAccounts = new ArrayList<>();
                    for (AccountService.AccountListResult.Value v : result.getValue()) {
                        if ("SEND".equalsIgnoreCase(v.getAccountTypeCd())/* && data.getFromCurrencyCd().equalsIgnoreCase(v.getCurrencyCd())*/) {
                            sendAccounts.add(v);
                        }
//                        accounts.add(v);
                    }
                    if (0 == sendAccounts.size()) {
                        showToast(R.string.txt_no_existed_pay_accounts_please_add);
                    } else {
                        String[] accountNames = new String[sendAccounts.size()];
                        for (int i = 0; i < sendAccounts.size(); i++) {
                            accountNames[i] = sendAccounts.get(i).getAccountName();
                        }
                        showListDialog(accountNames, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                selectedAccountWhich = which;
                                showScreen(sendAccounts.get(which));
                            }
                        });
                    }
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {

                }
            });
        }
    }

    private boolean checkThrought() {
        return selectedAccountWhich >= 0;
    }

    private void showScreen(AccountService.AccountListResult.Value v) {
//        if ("bank".equalsIgnoreCase(v.getPaymentTypeCd())) {
//
//        } else if ("card".equalsIgnoreCase(v.getPaymentTypeCd())) {
//
//        } else if ("zhiFuBao".equalsIgnoreCase(v.getPaymentTypeCd())) {
//
//        } else if ("paypal".equalsIgnoreCase(v.getPaymentTypeCd())) {
            contentDisplayArea.removeAllViewsInLayout();
            View view = LayoutInflater.from(this).inflate(R.layout.activity_exchange_step2_paypal, null);
            contentDisplayArea.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ((EditText) view.findViewById(R.id.account_name)).setText(v.getAccountName());
//        }
    }

    private void initView() {
        stepView = (StepView) findViewById(R.id.step_view);
        indicator0 = (View) findViewById(R.id.indicator_0);
        recordsExists = (RelativeLayout) findViewById(R.id.records_exists);
        recordsExists.setOnClickListener(this);
        contentDisplayArea = F(R.id.content_display_area);
        nextStep = (TextView) findViewById(R.id.next_step);
        nextStep.setOnClickListener(ExchangeStep2Activity.this);

        stepView.setCurrentStep(2);
    }

    @Override
    public void onEventMainThread(DPBusEvent event) {
        super.onEventMainThread(event);
        if (event.what == Consts.BusEventCommands.FINISH_EXCHANGE_STEP_2_AND_3) {
            finish();
        }
    }

    private void showListDialog(String[] items, DialogInterface.OnClickListener listener) {
        new AlertDialog.Builder(this).setItems(items, listener).show();
    }
}
